import service from './service'

//请求类型
type method = 'GET' | 'POST' | 'PUT' | 'DELETE'

// 后端数据返回格式
type Res<T> = {
  data: T | null
  message: string
  status: number | string
  totalCount?: number | null
}

/**
 * 统一接口请求方法
 * @param url 请求地址
 * @param params 请求参数
 * @param method 请求类型post、get、put、delete
 * @param headers 请求头
 * @returns Promise<Res<T>>
 */
const request = <T = any>(
  url: string,
  params: object,
  method: method = 'POST',
  headers?: object
): Promise<Res<T>> => {
  const propName = ['POST', 'PUT'].includes(method) ? 'data' : 'params'
  return service.request<any, Res<T>>({
    url,
    [propName]: params,
    method,
    headers
  })
}

export default request
